/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.news;

import java.util.*;
import java.io.*;
import bw.os.*;
import bw.util.*;

public class NewsCategory implements Serializable
{
    static final long serialVersionUID = 1397472054304195599L;

    private int _id = -1;
    private String _name = null;
    private Vector _sourceIds = new Vector();

    public NewsCategory ( int id, String name )
    {
        _id = id;
        _name = name;
    }

    public String getName () { return _name; }

    public int getId () { return _id; }

    public Vector getSourceIds () { return _sourceIds; }

    public int getNewItemsCount () 
    { 
        int newItems = 0;

        for ( Enumeration e = _sourceIds.elements(); e.hasMoreElements(); )
        {
            NewsSource n = getNewsSourceForId( (String) e.nextElement() );
            if ( n != null )
            {
                newItems += n.getNewItemsCount();
            }
        }

        return newItems;
    }

    public NewsSource getNewsSourceForId ( String id )
    {
        NewsSource source = null;

        try
        {
            //source = (NewsSource) ObjectStore.getInstance().get( NewsSource.getKey( id ) );
            source = (NewsSource) ObjectStore.getInstance().get( id );
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem getting news source: " + id, ex );
        }

        return source;
    }

    public Vector getNewsSources () 
    { 
        Vector sources = new Vector();

        for ( Enumeration e = _sourceIds.elements(); e.hasMoreElements(); )
        {
            NewsSource nd = getNewsSourceForId( (String) e.nextElement() );
            if ( nd != null )
            {
                sources.add( nd );
            }
        }

        return sources;
    }

    public void attachNewsSource ( NewsSource n )
    {
        _sourceIds.addElement( n.getId() );
    }

    public void addNewsSource ( NewsSource n ) 
    { 
        attachNewsSource( n );

        //save the news source for the first time
        n.sync();
    }

    public void clearNewsSources ()
    {
        //this removes all sources from my list AND deletes them
        //permanently from the ObjectStore
        Iterator sources = _sourceIds.iterator();
        while ( sources.hasNext() )
        {
            NewsSource ns = getNewsSourceForId( (String) sources.next() );
            if ( ns != null )
            {
                ns.destroy();
            }

            sources.remove();
        }
    }

    public void removeNewsSource ( NewsSource source )
    {
        //This method just removes this source from my 
        //list of sources
        _sourceIds.removeElement( source.getId() );
    }

    public void deleteNewsSource ( NewsSource source )
    {
        //this method actually deletes the newsSource from the
        //ObjectStore as well
        removeNewsSource( source );
        source.destroy();
    }

    public Vector getSortedNewsSources ()
    {
        Vector sources = getNewsSources();

        //sort the news sources by last refresh beginning 
        //with most recently refreshed. 
        Collections.sort( sources, new NewsSourceComparator() );

        return sources;
    }

    public void resetNewNewsItems ()
    {
        for ( Enumeration e = _sourceIds.elements(); e.hasMoreElements(); )
        {
            NewsSource s = getNewsSourceForId( (String) e.nextElement() );
            if ( s != null )
            {
                s.setNewNewsItemsSeen();
            }
        }
    }

    public boolean equals ( Object obj )
    {
        return ( getName().equals( ( (NewsCategory) obj ).getName() ) );
    }

    class NewsSourceComparator implements Comparator
    {
        public NewsSourceComparator()
        {
            //nothing
        }

        public int compare ( Object o1, Object o2 )
        {
            NewsSource a = (NewsSource) o1;
            NewsSource b = (NewsSource) o2;

            //reverse the sort order so that we sort beginning with most
            //recent instead of earliest.
            return b.getLastRefreshed().compareTo( a.getLastRefreshed() );
        }

        public boolean equals ( Object obj )
        {
            return ( this.hashCode() == obj.hashCode() );
        }
        
    }

}